var teacherid = getQueryVariable('techerid');
jQuery(document).ready(function() {
	$('#kit-search-more').on('click', function() {
		$('.kit-search-mored').toggle();
	});

	var col = [
	{
		formatter: function(value, row, index) {
			return index+1;
			}
	},{
			title: '时间段',
			field: 'name',
			align: 'center',
			valign: 'middle'
		},
		{
			title: '周一',
			align: 'center',
			valign: 'middle',
			formatter: function(value, row, index) {
				return '<div id="div_' + row.id + '_1"><input id="' + row.id + '_1" timeid="' + row.id + '" weekid="1"name="wait" type="checkbox" >&nbsp;&nbsp;请选择</div>';
			}
		},
		{
			title: '周二',
			align: 'center',
			valign: 'middle',
			formatter: function(value, row, index) {
				return '<div id="div_' + row.id + '_2"><input id="' + row.id + '_2" timeid="' + row.id + '" weekid="2"name="wait" type="checkbox" >&nbsp;&nbsp;请选择</div>';
			}
		},
		{
			title: '周三',
			align: 'center',
			valign: 'middle',
			formatter: function(value, row, index) {
				return '<div id="div_' + row.id + '_3"><input id="' + row.id + '_3" timeid="' + row.id + '" weekid="3"name="wait" type="checkbox" >&nbsp;&nbsp;请选择</div>';
			}
		},
		{
			title: '周四',
			align: 'center',
			valign: 'middle',
			formatter: function(value, row, index) {
				return '<div id="div_' + row.id + '_4"><input id="' + row.id + '_4" timeid="' + row.id + '" weekid="4"name="wait" type="checkbox" >&nbsp;&nbsp;请选择</div>';
			}
		},
		{
			title: '周五',
			align: 'center',
			valign: 'middle',
			formatter: function(value, row, index) {
				return '<div id="div_' + row.id + '_5"><input id="' + row.id + '_5" timeid="' + row.id + '" weekid="5"name="wait" type="checkbox" >&nbsp;&nbsp;请选择</div>';
			}
		},
		{
			title: '周六',
			align: 'center',
			valign: 'middle',
			formatter: function(value, row, index) {
				return '<div id="div_' + row.id + '_6"><input id="' + row.id + '_6" timeid="' + row.id + '" weekid="6"name="wait" type="checkbox" >&nbsp;&nbsp;请选择</div>';
			}
		},
		{
			title: '周日',
			align: 'center',
			valign: 'middle',
			formatter: function(value, row, index) {
				return '<div id="div_' + row.id + '_7"><input id="' + row.id + '_7" timeid="' + row.id + '" weekid="7"timeid="' + row.id + '" weekid="" name="wait" type="checkbox" >&nbsp;&nbsp;请选择</div>';
			}
		}
	];

	//------------------------------------------加载列表
	$('#_table').bootstrapTable({
		url: baseUrl + '/periods',
		'method': "GET",
		locale: 'zh-CN',
		detailView: false,
		sidePagination: "server", //服务端处理分页
		striped: true, //隔行变色
		undefinedText: "",
		pagination: true,
		singleSelect: false,
		pageSize: 200, //pageList[0],
		cache: false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
		pageNumber: 1,
		//		pageList: pageList,
		search: false, //不显示 搜索框
		uniqueId: "id", //每一行的唯一标识，一般为主键列
		showColumns: false, //不显示下拉框（选择显示的列）
		undefinedText: '',
		columns: col,
		queryParams: function(params) {
			return {
				pageNum: this.pageNumber,
				pageSize: this.pageSize,
				sortOrder: params.order
			}
		},
		onLoadSuccess: function() {
			loaddatas();
		}
	});

})

var loaddatas = function() {
	//url:'/teacherPeriods',
	$.get(baseUrl + '/teacherPeriods/used', {
		teacherid: teacherid
	}, function(data) {
		// console.log(data.rows.length);
		 datas = data.data;
		for(var i = 0; i < datas.length; i++){
            var htm = '';
            var obi = datas[i];
            var checkid = '';
            for(var j = 0; j < obi.length; j++){
                var obj = obi[j];
                var msg = obj.status;
                checkid=obj.checkid;

                switch(msg) {
                    case '保留':
                    	htm += '<input id="' + obj.checkid + '" objid="' + obj.id + '" weekid="' + obj.week + '"name="old" type="checkbox" checked>&nbsp;&nbsp;' +
                            '<font color="greenyellow">' + obj.useMsg + '</font><br/>';
                    		break;
                    case '预留':
                    	htm += '<input id="' + obj.checkid + '" objid="' + obj.id + '" weekid="' + obj.week + '"name="old" type="checkbox" checked>&nbsp;&nbsp;' + obj.useMsg+'<br/>';
                    		break;
                    case '占用':
                    	htm += '<input id="' + obj.checkid + '" objid="' + obj.id + '" weekid="' + obj.week + '"name="old" type="checkbox" checked>&nbsp;&nbsp;' +
                            '<font color="#EA2A31">' + obj.useMsg + '</font><br/>';
                    		break;
                    case '试听':
                        htm += '<input id="' + obj.checkid + '" objid="' + obj.id + '" weekid="' + obj.week + '"name="old" type="checkbox" checked>&nbsp;&nbsp;' +
                            '<font color="#F01EEF">' + obj.useMsg + '</font><br/>';
                        break;
                    default:
                    	htm += '<input id="' + obj.checkid + '" objid="' + obj.id + '" weekid="' + obj.week + '"name="old" type="checkbox" checked>&nbsp;&nbsp;' + obj.useMsg;
                    	htm += '<br/><input id="' + obj.checkid + '" objid="' + obj.id + '" weekid="' + obj.week + '"name="retention" type="checkbox">&nbsp;&nbsp;保留时段';
                    	break;
                }
			}
            $('#div_' + checkid).html(htm);
            $('#div_' + checkid).parent().css("background-color", '#9ACCF7');
        }
	});
}
var callback_save = function() {
	var adds = [],
		retentions = [],
		dels = [];
	var periodEdits = {};
	var news = $('input:checkbox[name=wait]:checked').each(function(i) {
		var obj = {};
		obj.teacherid = teacherid;
		obj.checkid = $(this).attr('timeid') + '_' + $(this).attr('weekid');
		obj.week = $(this).attr('weekid');
		obj.timeid = $(this).attr('timeid');
		obj.useMsg = '可用时段';
		obj.retention = '';
		adds.push(obj);
	});
	$('input:checkbox[name=old]:not(:checked)').each(function(i) {
		dels.push($(this).attr('objid'));
	});
	$('input:checkbox[name=retention]:checked').each(function(i) {
		var obj = {};
		obj.id = $(this).attr('objid');
		obj.status = '保留时段';
		retentions.push(obj);
	});
	console.log(retentions);
	console.log(dels);
	periodEdits.adds = adds;
	periodEdits.retentions = retentions;
	periodEdits.dels = dels;
	return JSON.stringify(periodEdits);
}

function doHidden() {

    $.get(baseUrl + '/periods/hidden', {
        teacherId: teacherid
    }, function (data) {
		var datas = data.data;
        for(var i = 0; i < datas.length; i++) {
            var timeId = datas[i];
            $('tr[data-uniqueid="' + timeId + '"]').hide();
        }
    });
}

function showAll() {
    $.get(baseUrl + '/periods/hidden', {
        teacherId: teacherid
    }, function (data) {
        var datas = data.data;
        for(var i = 0; i < datas.length; i++) {
            var timeId = datas[i];
            $('tr[data-uniqueid="' + timeId + '"]').show();
        }
    });
}